<{include file="public_html/header.html"}>
<{include file="public_html/left_menu.html"}>
<script type="text/javascript" charset="utf-8" src="/static/public/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/static/public/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="/static/public/ueditor/lang/zh-cn/zh-cn.js"></script>
<blockquote class="layui-elem-quote"><{$action}></blockquote>

<form class="layui-form layui-form-pane" method="post" action="<{$post_url}>" id="form_add_role">
	<input type="hidden" name="id" value="<{$topic_info['id']|default:'0'}>" />
	<div class="layui-form-item">
		<label class="layui-form-label">科目</label>
		<div class="layui-input-block">
			<select name="subject_id" id="subject" lay-filter="subject" lay-verify="required">
				<option value="0">请选择科目</option>
				<{foreach from=$subject item=item}>
					<option value="<{$item.id}>" <{if $topic_info['subject_id']|default:"33" eq $item.id}>selected="selected"<{/if}>><{$item.name}></option>
				<{/foreach}>
			</select>
		</div>
	</div>

		<div class="layui-form-item">
		<label class="layui-form-label">课本</label>
		<div class="layui-input-block">
			
			<select name="book_id" id="book" lay-filter="book" lay-verify="required">
				<option value="0">请选择课本</option>
				<{if isset($book_list) AND $book_list|count  > 0}>
				<{foreach from=$book_list item=book}>
					<option value="<{$book.id}>" <{if $book.id == $topic_info.book_id|default:"27"}> selected <{/if}>><{$book.title}></option>
				<{/foreach}>
				<{/if}>
			</select>
			<input type="hidden" id="book_name" name="book_name" />
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">章节</label>
			<div class="layui-input-block">
				<select name="chapter_id" id="chapter_id" lay-verify="required">
					<option value="0">请选择章节</option>
					<{if isset($chapter_list_data) AND $chapter_list_data|count  > 0}>
						<{foreach from=$chapter_list_data item=item}>
							<!-- <optgroup label="<{$item.title}>"> -->
							<option <{if $item.id == $topic_info.chapter_id|default:332}> selected <{/if}> value="<{$item.id}>"><{$item.title}></option>
							<{if $item.sub|count > 0}>
								<{foreach from=$item.sub item=item1}>
									<option <{if $item1.id == $topic_info.chapter_id|default:332}> selected <{/if}> value="<{$item1.id}>">----<{$item1.title}></option>
								<{/foreach}>
							<{/if}>
							<!-- </optgroup> -->
						<{/foreach}>
					<{/if}>
				</select>
			</div>
	</div>



	<div class="layui-form-item">
		<label class="layui-form-label">题目类型</label>
		<div class="layui-input-block">
			<select name="type" lay-verify="required" lay-filter="q_type" id="q_type" <{if $topic_info.type|default:"" > 0}> disabled <{/if}>>
				<option value="1" <{if 1 == $topic_info.type|default:""}> selected <{/if}>>单选题</option>
				<option value="2" <{if 2 == $topic_info.type|default:""}> selected <{/if}>>多选题</option>
				<option value="3" <{if 3 == $topic_info.type|default:""}> selected <{/if}>>判断题</option>
				<option value="4" <{if 4 == $topic_info.type|default:""}> selected <{/if}>>简答题</option>
			</select>
		</div>
	</div>

<!-- 	<div class="layui-form-item">
		<label class="layui-form-label">区域</label>
		<div class="layui-input-inline">
			<select name="province_code" id="province" lay-filter="province" lay-verify="required" lay-search>
				<option value="">请选择省</option>
				<{foreach from=$province_list item=item }>
				<option value="<{$item['area_code']}>" <{if $topic_info['province_code']|default:'440000' == $item['area_code'] }>selected<{/if}> ><{$item['area_name']}></option>
				<{/foreach}>
			</select>
		</div>
	   
		<div class="layui-input-inline">
			<select name="city_code" id="city" lay-filter="city" lay-verify="required" lay-search>
				<option value="">请选择市</option>
				<{if isset($city_list)}>
				<{foreach from=$city_list item=item }>
				<option value="<{$item['area_code']}>" <{if $topic_info['city_code']|default:'440300' == $item['area_code'] }>selected<{/if}> ><{$item['area_name']}></option>
				<{/foreach}>
				<{/if}>
			</select>
		</div>

		<input type="hidden" name="province_name" id="province_name" value="<{$topic_info['province_name']|default:''}>"/>
		<input type="hidden" name="city_name" id="city_name" value="<{$topic_info['city_name']|default:''}>"/>
	</div> -->

	<div class="layui-form-item">
		<label class="layui-form-label">难度等级</label>
		<div class="layui-input-block">
			<input type="radio" name="difficulty_level" value="1" title="难" <{if $topic_info.difficulty_level|default:"" == 1}> checked <{/if}>>
			<input type="radio" name="difficulty_level" value="2" title="中" <{if $topic_info.difficulty_level|default:"" == 2 || !isset($topic_info)}> checked <{/if}>>
			<input type="radio" name="difficulty_level" value="3" title="易" <{if $topic_info.difficulty_level|default:"" == 3}> checked <{/if}>>
		</div>
	</div>	

	<div class="layui-form-item layui-form-text" id="html_subject_text">
		<label class="layui-form-label">题目内容<span id="tiankong_tishi" style="color:red; padding-left: 10px;">提示：填空题的题目内容中每4个“_”(例:____，)符作为一个题目答案选项</span></label>
		<div class="layui-input-block">
			<!-- 加载编辑器的容器 -->
		    <script id="container" name="subject_text" type="text/plain">
		        <{$topic_info.subject_text|default:""}>
		    </script>
		    <!-- 配置文件 -->
		    <script type="text/javascript" src="/static/public/ueditor/ueditor.config.js"></script>
		    <!-- 编辑器源码文件 -->
		    <script type="text/javascript" src="/static/public/ueditor/ueditor.all.js"></script>
		    <!-- 实例化编辑器 -->
		    <script type="text/javascript">
		        var ue = UE.getEditor('container');
		    </script>
		</div>
	</div>

	<div class="layui-form-item" id="text_subject_text" style="display: none;">
		<label class="layui-form-label">题目内容</label>
		<div class="layui-input-block">
			<{if isset($topic_info)}>
				<input type="text" name="subject_text" value="<{$topic_info.subject_text}>" disabled="disabled" autocomplete="off" class="layui-input">
			<{else}>
				<input type="text" name="subject_text" value="" autocomplete="off" class="layui-input" disabled="disabled">
			<{/if}>
		</div>
	</div>

	<div class="layui-form-item layui-form-text">
		<label class="layui-form-label">答案解析</label>
		<div class="layui-input-block">
			<textarea class="layui-textarea layui-hide editor" name="analysis" lay-verify="content" id="editor2"><{$topic_info.analysis|default:""}></textarea>
		</div>
	</div>

	<div class="layui-input-block">
		<{if $topic_info['id']|default:'0' <= 0}>
		<button class="layui-btn layui-btn-warm" lay-submit lay-filter="sub_topic" name="save_add_items">保存并添加子题目</button>
		<button class="layui-btn layui-btn-danger" lay-submit lay-filter="sub_topic" name="save_add_answers">保存并直接添加答案</button>
		<{else}>
		<button class="layui-btn" lay-submit lay-filter="sub_topic">保存</button>
		<{/if}>
	</div>
</form>


<{include file="public_html/footer.html"}>
<script>
	$(function(){
		$(".sub-colum").find('li').on('click',function(){//子栏目点击选中状态；
			$(this).addClass('curr').siblings().removeClass('curr');
		})
		$("#add_test_volume").click(function(){
			location.href = "/tiku/add_test_volume";
		})

	});

	layui.use(['form', 'jquery', 'layedit', 'element'], function(){
		var layedit = layui.layedit,form = layui.form(),$ = layui.jquery, element = layui.element();
		/**
		 * book_id
		 * @param  {[type]} data){			var ii                                                         [description]
		 * @param  {[type]} success:      function(result){					layer.close(ii);					if(result.code [description]
		 * @return {[type]}               [description]
		 */
		form.on('select(subject)', function(data){
			var ii = layer.load();
			var subject_id = $(data.elem).val();
			$("#book").html('<option value="0">请选择课本</option>');
			form.render('select');
			$.ajax({
				type: "POST",
				url: "/topic/ignore_get_books",
				dataType:"json",
				data:{subject_id:subject_id},
				success: function(result){
					layer.close(ii);
					if(result.code == 1){
						var html ='';
						$.each(result.chapter_list, function(i, obj) {
							$("#book").append('<option value="'+obj.id+'">'+obj.title+'</option>');
						});
						
						form.render('select');
					}
				}
			});
		});

		form.on('select(book)', function(data){
			var ii = layer.load();
			var book_id = $(data.elem).val();
			$("#chapter_id").html('<option value="0">请选择章节</option>');
			form.render('select');
			$.ajax({
				type: "POST",
				url: "/topic/ignore_get_book_chapter",
				dataType:"json",
				data:{book_id:book_id},
				success: function(result){
					layer.close(ii);
					if(result.code == 1){
						var html ='';
						$.each(result.chapter_list, function(i, obj) {
							// html += '<optgroup label="'+ obj.title+'">';
							/*if(obj.sub){
                                $.each(obj.sub, function(j, obj1) {
                                	html += '<option value="'+obj1.id+'">'+obj1.title+'</option>';
                                });
                            }*/
                            html += '<option value="'+obj.id+'">'+obj.title+'</option>';
                            if(obj.sub){
                                $.each(obj.sub, function(j, obj1) {
                                	html += '<option value="'+obj1.id+'">----'+obj1.title+'</option>';
                                });
                            }
                            // html += '</optgroup>'
						});
						$("#chapter_id").append(html);
						$("#book_name").val($("#book option:selected").text())
						form.render('select');
					}
				}
			});
		});

		form.on('select(province)',function(data){
			var ii = layer.load();
			var province_code = data.value;
			$("#province_name").val($("#province option:selected").text())
			$("#city_name").val('');
			$("#area_name").val('');
			$("#city").html('<option value="">请选择城市</option>');
			$("#district").html('<option value="">请选择区域</option>');
			form.render('select');
			$.ajax({
				type: "GET",
				url: "/area/get_area_by_parent/"+province_code,
				dataType:"json",
				success: function(result){
					var html ='';
					$.each(result, function(i, obj) {
						html += '<option value="'+obj.area_code+'">'+obj.area_name+'</option>';
					});
					$("#city").append(html);
					form.render('select');
					layer.close(ii);
				}
			});
		});

		layui.form().on('select(q_type)', function(data){
			var type = $(data.elem).val();
			if(type == 6){
				$("#text_subject_text").show();
				$("#text_subject_text").find("input").attr("disabled", false);
				$("#text_subject_text").find("input").attr("lay-verify", "required");
				$("#html_subject_text").hide();
				$("#html_subject_text").find("textarea").attr("disabled", true);
			}else{
				$("#text_subject_text").hide();
				$("#text_subject_text").find("input").attr("disabled", true);
				$("#text_subject_text").find("input").attr("lay-verify", "");
				$("#html_subject_text").show();
				$("#html_subject_text").find("textarea").attr("disabled", false);
			}
		});

		form.on('select(city)',function(data){
			$("#city_name").val($("#city option:selected").text())
		});
	});
</script>